using Gateway.IQuery.Order;
using Gateway.IQuery.Order.Result;
using Gateway.IRepositories;
using SqlSugar;

namespace Gateway.Query;

/// <summary>
/// 
/// </summary>
public class OrderQuery : IOrderQuery
{
    private readonly IQueryRepository _queryRepository;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="queryRepository"></param>
    public OrderQuery(IQueryRepository queryRepository)
    {
        _queryRepository = queryRepository;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<object> GetDetail(string id)
    {
        var data = await _queryRepository.FindSplitAsync<OrderDetailResult>(x => x.OrderId.Equals(id));
        return data;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="area"></param>
    /// <param name="page"></param>
    /// <param name="size"></param>
    /// <returns></returns>
    public async Task<object> GetList(string area, int page, int size)
    {
        RefAsync<int> total = 0;
        return await _queryRepository.Db.Queryable<OrderDetailResult>()
            .WhereIF(!string.IsNullOrEmpty(area), x => x.Area.Equals(area))
            .OrderByDescending(x => x.OrderId)
            .ToPageListAsync(page, size, total);
    }
}